﻿using System;
using System.Web.UI;
using Project.Model;
using Project.Common;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using NHibernate.Criterion;

namespace Web
{
	/// <summary>
	/// 功能：编辑页面
	///       (Plantable)
	/// 作者：luky
	/// 日期：2010-12-28
	/// </summary>
    public partial class Plantable_Edit : AdminPage
    {
        /// <summary>
        /// 计划类别 Id
        /// </summary>
        protected Int32 PID { get { int _pid = 0; int.TryParse(base.QueryString("pid"), out _pid); return _pid; } }
        /// <summary>
        /// 计划类别
        /// </summary>
        protected string JhType { get { return base.QueryString("jhtype"); } }
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                IList<ICriterion> queryList = new List<ICriterion>();
                queryList.Add(Restrictions.Eq(PropertyNameHelper.Resolve<Plantable>(d => d.Jlyear), "2012"));
                //此处添加需要绑定的控件
                base.BindEnum2WebControls(Chsh_managementDropDownList, typeof(EnumChsh_management), false, 0);
                JhTypeDropDownList.DataSource = ManagerFactory.DPlantableManager.GetList(queryList);

                JhTypeDropDownList.DataTextField = PropertyNameHelper.Resolve<DPlantable>(d => d.JhType);
                JhTypeDropDownList.DataValueField = PropertyNameHelper.Resolve<DPlantable>(d => d.JhType);
                //JhTypeDropDownList.DataValueField = PropertyNameHelper.Resolve<DPlantable>();
                JhTypeDropDownList.DataBind();
                JhTypeDropDownList.Items.Insert(0,new ListItem("请选择",""));

                if (PID > 0)
                {
                    DPlantable modelDPlantable = ManagerFactory.DPlantableManager.Get(PID);
                    if (modelDPlantable != null && JhTypeDropDownList.Items.FindByValue(modelDPlantable.JhType) != null)
                    {
                        JhTypeDropDownList.Items.FindByValue(modelDPlantable.JhType).Selected = true;
                    }
                }
                if (!string.IsNullOrEmpty(JhType) && JhTypeDropDownList.Items.FindByValue(JhType) != null)
                {
                    JhTypeDropDownList.Items.FindByValue(JhType).Selected = true;
                }

                if (this.SID > 0)
                {
                    // 编辑状态
                    Plantable model = ManagerFactory.PlantableManager.Get(this.SID);
                    model.Jlyear = DateTime.Now.Year.ToString();
                    
                    if (model == null)
                    {
                        Alert("记录不存在!");
                        ExecJsAjax("window.close();", Page);
                        return;
                    }

                    // 绑定页面控件与对象的值
                    base.BindControlsValue(Master.FindControl("ContentPlaceHolder1").Controls, model, true);
                    
                    // 以下添加页面控件自定义显示代码
                    
                }
                else
                {
                    // 新建
                }

                // 查看
                if (Request.Params["isenable"] != null)
                {
                    ClientScript.RegisterStartupScript(typeof(Page), "a", "<script>ChangeToLabel();</script>");
                }

            }

        }

        protected void saveButton_Click(object sender, System.EventArgs e)
        {
            if (IsValid)
            {
                Plantable model = new Plantable();

                try
                {
                    if (this.SID > 0)
                    {
                       
                            model = ManagerFactory.PlantableManager.Get(this.SID);
                       
                    }
                    else
                    {
                        // 新建
                    }

                    if (model == null)
                    {
                        Alert("记录不存在!");
                        ExecJsAjax("window.close();", Page);
                        return;
                    }

                    // 绑定页面控件与对象的值
                    model = base.BindControlsValue(Master.FindControl("ContentPlaceHolder1").Controls, model, false);

                    if (this.SID > 0)
                    {
                        // 更新
                       
                            ManagerFactory.PlantableManager.Update(model);
                        
                      
                    }
                    else
                    {
                        // 新建
                        model.Jlyear = DateTime.Now.Year.ToString();
                        
                        ManagerFactory.PlantableManager.Save(model);
                    }
                    Alert("保存成功!");
                    ExecJsAjax("window.opener.searchRefresh();window.close();", Page);
                }
                catch (Exception ex)
                {
                    Alert(ex.Message);
                }
            }
        }

        #region 前端验证

        protected void Chsh_managementCustomValidator_ServerValidate(object source, ServerValidateEventArgs args)
        {
            args.IsValid = Chsh_managementDropDownList.SelectedIndex > 0;
        }

        #endregion
    }
}